<template>
  <el-dialog
    width="1200px"
    :title="$t('库存记录')"
    destroy-on-close
    append-to-body
    :visible.sync="show"
    :before-close="cancel"
  >
    <el-row style="margin-bottom: 10px">
      <el-form :model="queryParams" ref="queryForm" size="small" inline v-show="showSearch">
        <el-form-item>
          <el-select @change="getList" clearable :placeholder="$t('类型')" v-model="queryParams.type">
            <el-option
              v-for="item in sku_consumable_type_list"
              :key="item.dictValue"
              :value="item.dictValue"
              :label="item.dictLabel"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="getList">
            {{ $t('搜索') }}
          </el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">
            {{ $t('重置') }}
          </el-button>
        </el-form-item>
      </el-form>
    </el-row>

    <el-table
      :height="calTableHeight() - 150"
      ref="TableRef"
      :data="tableData"
      @selection-change="(rows) => (tableCheck = rows)"
    >
      <el-table-column show-overflow-tooltip align="center" :label="$t('耗材名称')" prop="cesName" />
      <el-table-column show-overflow-tooltip align="center" :label="$t('sku编号')" prop="skuNo" />
      <el-table-column show-overflow-tooltip align="center" :label="$t('类型')" prop="type">
        <template #default="{ row }">
          <div :style="{ color: $randomColor(row.type) }">{{ handleType(row.type) }}</div>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip align="center" :label="$t('操作数量')" prop="num" />
      <el-table-column show-overflow-tooltip align="center" :label="$t('操作前数量')" prop="beforeNum" />
      <el-table-column show-overflow-tooltip align="center" :label="$t('操作后数量')" prop="afterNum" />
      <el-table-column show-overflow-tooltip align="center" :label="$t('备注')" prop="remark" />
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <div slot="footer">
      <el-button @click="cancel">
        {{ $t('关闭') }}
      </el-button>
    </div>
  </el-dialog>
</template>
<script>
import { getSkuConsumableRecordList } from '@/api/distribution/sku/skuConsumable'
import { useDict } from '@/utils/useDict'

export default {
  props: {
    /**
     * formData
     */
    formData: {
      type: Object,
      default: () => ({}),
    },
    /**
     * type
     */
    type: {
      type: String,
    },
    /**
     * 控制显示隐藏
     */
    show: {
      type: Boolean,
      required: true,
      default: false,
    },
  },
  data() {
    return {
      /**显示搜索条件 */
      showSearch: true,
      /**表格选中项 */
      tableCheck: [],
      sku_consumable_type_list: [],
      /**表格数据 */
      tableData: [],
      loading: false,
      /**总条数 */
      total: 0,
      /**分页相关查询参数 */
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
    }
  },
  computed: {},
  watch: {
    show: function (newValue) {
      if (newValue) {
        useDict('sku_consumable_type').then((res) => {
          this.sku_consumable_type_list = res.sku_consumable_type
        })
        this.getList()
      }
    },
  },
  methods: {
    handleType(type) {
      const index = this.sku_consumable_type_list.findIndex((i) => i.dictValue == type)
      return this.sku_consumable_type_list[index]?.dictLabel
    },
    handleSubmit() {
      // TODO
      this.$refs['formRef'].validate((valid) => {
        if (valid) {
        }
      })
      this.cancel()
    },
    getList() {
      this.loading = true
      getSkuConsumableRecordList({
        ...this.queryParams,
        cesId: this.formData.id,
      })
        .then((res) => {
          this.tableData = res.rows
          this.total = res.total
        })
        .finally(() => (this.loading = false))
    },

    cancel() {
      this.$emit('update:show', false)
    },
    /** 重置按钮操作 */
    resetQuery() {
      Object.keys(this.queryParams).forEach((key) => (this.queryParams[key] = null))
      this.queryParams.pageNum = 1
      this.queryParams.pageSize = 10
      this.getList()
    },
  },
}
</script>
<style scoped lang="scss"></style>
